package Demo1;

//判断出栈顺序有没有可能
// [1,2,3,4,5]
// [4,5,3,2,1]

//[1,2,3,4,5]
//[4,3,5,1,2]

//一个是栈 另一个是队列

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class t4 {
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> into = new Stack<>();
        Queue<Integer> out = new LinkedList<>();
        for (int i = 0; i < popV.length; i++) {
            out.offer(popV[i]);
        }
        int k = 0;
        into.push(pushV[k++]);
        while (!out.isEmpty() && !into.isEmpty()) {
            if(into.peek().equals(out.peek())) {
                out.poll();
                into.pop();
            } else {
                if(k < pushV.length) {
                    into.push(pushV[k++]);
                } else {
                    return false;
                }
            }
//            if(into.isEmpty() && k < pushV.length) {
//                into.push(pushV[k++]);
//            }
        }

        return true;
    }

    public static void main(String[] args) {
        int[] a = {2,1,0};  //
        int[] b = {1,2,0};  //0
        System.out.println(IsPopOrder(a, b));
    }
}
